package com.tjec.project.system.controller;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.system.domain.BmsMdDocCatalogTemplate;
import com.tjec.project.system.domain.vo.BmsAttachmentVo;
import com.tjec.project.system.service.IBmsAttachmentService;
import com.tjec.project.system.service.IBmsMdDocCatalogTemplateService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目文档目录模板Controller
 *
 * @author tjec
 * @date 2020-04-16
 */
@RestController
@RequestMapping("/system/docCatalogTemplate")
public class BmsMdDocCatalogTemplateController extends BaseController {
    @Value("${template.url}")
    private String url;
    @Value("${template.doc_catalog_template}")
    private String template;
    @Value("${tjec.profile}")
    private String profile;

    @Autowired
    private IBmsMdDocCatalogTemplateService bmsMdDocCatalogTemplateService;
    @Autowired
    private IBmsAttachmentService iBmsAttachmentService;

    /**
     * 查询项目文档目录模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:docCatalogTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsMdDocCatalogTemplate bmsMdDocCatalogTemplate) {
        startPage();
        List<BmsMdDocCatalogTemplate> list = bmsMdDocCatalogTemplateService.selectBmsMdDocCatalogTemplateList(bmsMdDocCatalogTemplate);
        return getDataTable(list);
    }

    /**
     * 导出项目文档目录模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:docCatalogTemplate:export')")
    @Log(title = "项目文档目录模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsMdDocCatalogTemplate bmsMdDocCatalogTemplate) {
        List<BmsMdDocCatalogTemplate> list = bmsMdDocCatalogTemplateService.selectBmsMdDocCatalogTemplateList(bmsMdDocCatalogTemplate);
        ExcelUtil<BmsMdDocCatalogTemplate> util = new ExcelUtil<BmsMdDocCatalogTemplate>(BmsMdDocCatalogTemplate.class);
        return util.exportExcel(list, "docCatalogTemplate");
    }

    /**
     * 下载模板文件
     */
    @PreAuthorize("@ss.hasPermi('system:docCatalogTemplate:export')")
    @Log(title = "下载模板文件", businessType = BusinessType.EXPORT)
    @GetMapping("/exportTemplate")
    public AjaxResult exportTemplate(HttpServletRequest request) {
        Map map = new HashMap<>();
        map.put("url", Constants.RESOURCE_PREFIX + url + "/" + template);
        return AjaxResult.success(map);
    }

    /**
     * 获取项目文档目录模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:docCatalogTemplate:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid) {
        return AjaxResult.success(bmsMdDocCatalogTemplateService.selectBmsMdDocCatalogTemplateById(guid));
    }

    /**
     * 新增项目文档目录模板
     */
    @PreAuthorize("@ss.hasPermi('system:docCatalogTemplate:add')")
    @Log(title = "项目文档目录模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsMdDocCatalogTemplate bmsMdDocCatalogTemplate) {
        return toAjax(bmsMdDocCatalogTemplateService.insertBmsMdDocCatalogTemplate(bmsMdDocCatalogTemplate));
    }

    /**
     * 修改项目文档目录模板
     */
    @PreAuthorize("@ss.hasPermi('system:docCatalogTemplate:edit')")
    @Log(title = "项目文档目录模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsMdDocCatalogTemplate bmsMdDocCatalogTemplate) {
        return toAjax(bmsMdDocCatalogTemplateService.updateBmsMdDocCatalogTemplate(bmsMdDocCatalogTemplate));
    }

    /**
     * 删除项目文档目录模板
     */
    @PreAuthorize("@ss.hasPermi('system:docCatalogTemplate:remove')")
    @Log(title = "项目文档目录模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(bmsMdDocCatalogTemplateService.deleteBmsMdDocCatalogTemplateByIds(guids));
    }

    /**
     * 验证文件数据格式
     */
    @GetMapping(value = "/validation")
    public AjaxResult validation(String guid) throws Exception {
        //根据id获取文件路径
        List<BmsAttachmentVo> list = iBmsAttachmentService.selectBmsAttachmentDownloadabsolute(guid);
        if (CollectionUtils.isEmpty(list)) {
            AjaxResult.error("系统不存在该文件");
        }
        String fullPath = profile + list.get(0).getStoragePath();
        return bmsMdDocCatalogTemplateService.validation(fullPath);
    }
    
    @GetMapping(value = "/getCatalogCounts/{businessId}")
    public AjaxResult getCatalogCounts(@PathVariable("businessId") String businessId) {
        return AjaxResult.success(bmsMdDocCatalogTemplateService.getCatalogCounts(businessId));
    }
}
